package com.cn.aam.checaiduo.ui.me.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.bean.me.VoucherB;
import com.cn.aam.checaiduo.ui.me.fragment.VoucherFragment;
import com.cn.aam.checaiduo.ui.me.helper.MeHelper;
import com.cn.aam.checaiduo.utils.StringUtil;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.cn.aam.checaiduo.utils.CalendarUtil.getDateString;

/**
 * monker on 2017/6/5.
 */

public class VoucherAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private int type;
    private VoucherB voucherB;
    private Context context;

    private int expiredPosition = Integer.MAX_VALUE;

    public VoucherAdapter(Context context, VoucherB voucherB, int type) {
        this.context = context;
        this.voucherB = voucherB;
        this.type = type;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        if (viewType == 1) {
            view = inflater.inflate(R.layout.list_voucher_item, parent, false);
            return new CashCouponHolder(view);
        } else {
            view = inflater.inflate(R.layout.layout_voucher_section, parent, false);
            return new SectionHolter(view);
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (position > expiredPosition) position = position - 1;
        if (viewHolder instanceof CashCouponHolder) {
            CashCouponHolder holder = (CashCouponHolder) viewHolder;
            if (type == VoucherFragment.CASH_COUPON) {
                VoucherB.CashCouponBean bean = voucherB.getCash_coupon().get(position);
                holder.tvTypeName.setText("代金券");
//                String note = "满" + bean.getMini_invest() + "元可用";
                holder.tvDescription.setText(bean.getDescription());
                holder.tvTime.setText(String.format("%s一%s", getDateString(bean.getCreate_time()), getDateString(bean.getExpired_time())));
                holder.tvRate.setText(StringUtil.decimalRemoval(bean.getAmount()));
                holder.tvTag.setVisibility(View.VISIBLE);
                if (position >= expiredPosition) {//过期
                    holder.tvTypeName.setTextColor(ContextCompat.getColor(context, R.color.black_alpha40));
                    holder.tvTime.setTextColor(ContextCompat.getColor(context, R.color.black_alpha40));
                    holder.tvRate.setTextColor(ContextCompat.getColor(context, R.color.black_alpha40));
                    holder.rlParent.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_voucher_cash_expired));
                } else {//未过期

                    holder.tvTypeName.setTextColor(ContextCompat.getColor(context, R.color.black_alpha70));
                    holder.tvTime.setTextColor(ContextCompat.getColor(context, R.color.black_alpha70));
                    holder.tvRate.setTextColor(ContextCompat.getColor(context, R.color.black_alpha70));
                    holder.rlParent.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_voucher_cash_coupon));
                }
            } else {
                VoucherB.RateCouponBean bean = voucherB.getRate_coupon().get(position);
                holder.tvTypeName.setText("加息券");
                holder.tvDescription.setText(bean.getDescription());
                holder.tvTime.setText(String.format("%s一%s", getDateString(bean.getCreate_time()), getDateString(bean.getExpired_time())));
                holder.tvRate.setText(String.format("%s%%", StringUtil.dealDataEnd(bean.getRate())));
                holder.tvTag.setVisibility(View.INVISIBLE);
                if (position >= expiredPosition) {//过期
                    holder.rlParent.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_voucher_rate_expired));
                    holder.tvTypeName.setTextColor(ContextCompat.getColor(context, R.color.black_alpha40));
                    holder.tvTime.setTextColor(ContextCompat.getColor(context, R.color.black_alpha40));
                    holder.tvRate.setTextColor(ContextCompat.getColor(context, R.color.black_alpha40));
                } else {//未过期
                    holder.tvTypeName.setTextColor(ContextCompat.getColor(context, R.color.black_alpha70));
                    holder.tvTime.setTextColor(ContextCompat.getColor(context, R.color.black_alpha70));
                    holder.tvRate.setTextColor(ContextCompat.getColor(context, R.color.black_alpha70));
                    holder.rlParent.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_voucher_rate_coupon));
                }
            }
        }


    }

    @Override
    public int getItemViewType(int position) {
        if (expiredPosition != -1 && position == expiredPosition) {
            return 2;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        int size = 0;
        int expired = expiredItem();
        if (type == VoucherFragment.CASH_COUPON) {
            size = voucherB.getCash_coupon().size();
        } else {
            size = voucherB.getRate_coupon().size();
        }
        return (size + expired);
    }


    public class CashCouponHolder extends RecyclerView.ViewHolder {
        public View itemView;
        @BindView(R.id.voucher_tv_typeName)
        TextView tvTypeName;
        @BindView(R.id.voucher_tv_rate)
        TextView tvRate;
        @BindView(R.id.voucher_tv_time)
        TextView tvTime;
        @BindView(R.id.voucher_rl_parent)
        RelativeLayout rlParent;
        @BindView(R.id.voucher_tv_description)
        TextView tvDescription;
        @BindView(R.id.voucher_tv_tag2)
        TextView tvTag;
        @BindView(R.id.voucher_ll_check)
        LinearLayout llCheck;

        public CashCouponHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
            tvTag.setVisibility(View.GONE);
            llCheck.setVisibility(View.GONE);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

    public class SectionHolter extends RecyclerView.ViewHolder {

        public SectionHolter(View itemView) {
            super(itemView);
        }
    }

    /**
     * 时间：2017/6/7
     * 描述：是否有过期的理财券，有的话记录过期理财券的position,在此处需要加一个分割布局
     */
    private int expiredItem() {
        if (type == VoucherFragment.CASH_COUPON) {
            for (int i = 0; i < voucherB.getCash_coupon().size(); i++) {
                String used = voucherB.getCash_coupon().get(i).getUsed();
                String expired = voucherB.getCash_coupon().get(i).getExpired();

                if (TextUtils.equals(MeHelper.voucherCanUse(used, expired), MeHelper.VOUCHER_CANNOT_USED)) {
                    expiredPosition = i;
                    return 1;
                }
            }
        } else {
            for (int i = 0; i < voucherB.getRate_coupon().size(); i++) {
                String used = voucherB.getRate_coupon().get(i).getUsed();
                String expired = voucherB.getRate_coupon().get(i).getExpired();
                if (TextUtils.equals(MeHelper.voucherCanUse(used, expired), MeHelper.VOUCHER_CANNOT_USED)) {
                    expiredPosition = i;
                    return 1;
                }
            }
        }
        return 0;
    }


}
